import 'package:get/get.dart';
import 'package:honeypotflutter/modules/deliver/api/deliver_api.dart';
import 'package:honeypotflutter/modules/deliver/pages/deliver_order/view.dart';
import 'package:honeypotflutter/modules/deliver/views/deliver_forbid_bottom.dart';
import 'package:honeypotflutter/public/public_location.dart';
import 'package:honeypotflutter/public/public_storage.dart';
import 'package:honeypotflutter/publicBussine/loginInfo/bussine_userInfo.dart';
import 'package:modal_bottom_sheet/modal_bottom_sheet.dart';
import '../../help/deliver_help.dart';
import '../../model/deliver_boxInfo_model.dart';

class DeliverSelectLogic extends GetxController {
  late DeliverBoxInfoModel infoModel;

  UserInfoLogic _userInfoLogic = Get.find<UserInfoLogic>();

  bool isShow = false;
  String _key = "deliver_first_show";

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    isShow = HLStorage.getStorage<bool>(_key) ?? false;
  }

  /*
  * 请求详情
  * */
  requestDeliverBoxInfo(String? qr) async {
    if (qr == null) {
      return;
    }
    try {
      infoModel = await DeliverApi.boxInfo(qr);
      if (infoModel.isUpdateLocation == true) {
        startLocation();
      }
      update();
    } catch (error) {
    } finally {
      showGuidWidget();
    }
  }

  /*
  * 开启定位
  * */
  startLocation() async {
    var p = await HLLocation.startLocation();
    await DeliverApi.location(deviceId: infoModel.deviceId, lat: p.latitude, lng: p.longitude);
  }

  /*
  * 去投递订单详情
  * */
  pushToOrder({DeliverBoxType type = DeliverBoxType.max}) {
    Get.to(DeliverOrderPage(), arguments: {
      "disPlayNo": infoModel.disPlayNo,
      "deviceId": infoModel.deviceId,
      "type": type,
    });
  }

  /*
  * 显示提示
  * */
  showGuidWidget() async {
    if (isShow) {
      return;
    }
    Future.delayed(Duration(seconds: 2), () {
      showCupertinoModalBottomSheet(
        context: Get.context!,
        builder: (context) => DeliverForbidBottom(action: () {
          HLStorage.setStorage(_key, true);
        }),
      );
    });
  }
}
